//
//  JXManager.h
//  JXIntercomSDK
//
//  Created by Nansen on 2020/3/26.
//  Copyright © 2020 jingxi. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "JXIntercomConstants.h"
#import "JXConnectingManager.h"


NS_ASSUME_NONNULL_BEGIN

@class JXManagerConfig;
@class JXDoorDeviceModel;
@class JXExtDeviceModel;

@class JXHistoryManager;
@class JXSecurityManager;
@class JXDeviceManager;

@class CATiledLayer;

@protocol JXIntercomDelegate <NSObject>

@required
/// 有呼叫进来, 是否需要响应.
- (BOOL)shouldResponseIntercomCall:(JX_IntercomScenes)callScenes;

@optional
/// 被门禁呼叫 (shouldResponseIntercomCall 返回 YES 的时候才会有)
- (id<JXConnectingDelegate> _Nullable)calledByDoorWithHomeId:(NSString *)homeId
                                                   sessionId:(NSString *)sessionId
                                                  doorDevice:(JXDoorDeviceModel *)doorDevice;

/// 被室内通呼叫 (shouldResponseIntercomCall 返回 YES 的时候才会有)
- (id<JXConnectingDelegate> _Nullable)calledByExtWithHomeId:(NSString *)homeId
                                                  sessionId:(NSString *)sessionId
                                                  extDevice:(JXExtDeviceModel *)extDevice;

/// 被户户通呼叫 (shouldResponseIntercomCall 返回 YES 的时候才会有)
/// @param homeId homeId
/// @param sessionId 会话的 sessionId
/// @param callNumber 呼叫的房号
- (id<JXConnectingDelegate> _Nullable)calledByP2PWithHomeId:(NSString *)homeId
                                                  sessionId:(NSString *)sessionId
                                                 callNumber:(NSString *)callNumber;

/// 鉴权的结果
/// @param result 参照 DeviceActiveResult 说明
/// @param message message
- (void)onDeviceActivated:(DeviceActiveResult)result message:(NSString * _Nullable)message;

@end



@interface JXManager : NSObject

/// 用来显示的昵称
@property (nonatomic, copy, nullable, readonly) NSString *alias;
/// 移动端用账号 systemId, sn
@property (nonatomic, copy, nullable, readonly) NSString *userId;
/// buttonKey, 需要使用可以单独配置. 默认是 nil
@property (nonatomic, copy, nullable, readonly) NSString *buttonKey;
/// APNS 推送所使用的 ID, 配置成开发者对接的推送服务所用的 id. 后台会将推送消息推送至此 id.
@property (nonatomic, copy, nullable, readonly) NSString *pushId;


+ (instancetype)defaultManage;

+ (NSString *)version;

@property (nonatomic, weak, nullable) id<JXIntercomDelegate> appDelegate;

/// 默认配置
@property (nonatomic, strong, nullable) JXManagerConfig *config;

/// 历史记录相关
@property (nonatomic, strong, readonly) JXHistoryManager *historyManager;

/// 安防相关
@property (nonatomic, strong, readonly) JXSecurityManager *securityManager;

/// 设备管理相关
@property (nonatomic, strong, readonly) JXDeviceManager *deviceManager;

/// 通话相关
@property (nonatomic, strong, readonly) JXConnectingManager *connectingManager;

/// SDK初始化配置, 全局只需要调用一次
- (void)startWithConfig:(JXManagerConfig *)config;

/// 配置推送的 ID, 建议在 startHome: 之前进行配置完成
- (void)deployAPNsID:(NSString *)pushId;

/// 收到推送的消息
/// @param pushInfo 推送的消息体
/**
 举例:
 {
     ack = 1;
     body = "callout_success";
     cid = e177735752c068c1e10df0fb1352889e;
     cmd = call;
     device = "y_m_18114222751_";
     fid = 00200521C8DA0000;
     message = "CgRpY29tEgRjYWxsGmoKIDllMTQ1NjZhMmExM2VmOGY1ZDRiNDhkZTdiYzdhMDhhEhAwMDIwMDUyMUM4REEwMDAwGAIgASgDMAJCCeWwj+ahlOWtkFoQeV9tXzE4MDIxNjUxODEyX2oPeV9tXzE4MDIxNjUxODEyIiBlMTc3NzM1NzUyYzA2OGMxZTEwZGYwZmIxMzUyODg5ZSgCOAJKrAh7ImFjayI6dHJ1ZSwiYXVkaW8iOnRydWUsImNsaWVudF9pZCI6IiIsImNtZCI6ImNhbGwiLCJjb250YWN0Ijp7ImFjY291bnQiOiI8c2lwOmY5NTY5YzRlOGY4NWY5ZDBhZmE2MTNjYTY1MGNiOWNlQDQ3LjEwMC4yMDMuMjY6MTg4ODg+IiwiaWNlIjoiZXlKaFoyZHlaWE56YVhabElqcG1ZV3h6WlN3aWJHOXZjR0ZrWkhJaU9tWmhiSE5sTENKemRIVnVYM05sY25abGNpSTZJalEzTGpFd01D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DU2NmEyYTEzZWY4ZjVkNGI0OGRlN2JjN2EwOGEiLCJ2ZXJzaW9uIjoyLCJ2aWRlbyI6dHJ1ZX0=";
     platform = 4;
     result = 0;
     scheme = icom;
     title = icom;
 }
 */
- (void)receivePushNoti:(NSDictionary *)pushInfo;



/// 登录
/// @param userId 移动端的账号,代表移动端的唯一值
/// @param alias 用来显示的昵称. 传空值则默认为"iPhone"
/// @param buttonKey 统一配置(可选).
- (BOOL)loginWithUserId:(NSString *)userId
                  alias:(NSString *)alias
              buttonKey:(NSString * _Nullable)buttonKey;


/// 激活云端代理功能. 在登录之后调用
/// 结果由 JXIntercomDelegate 的 onDeviceActivated:message: 方法提供
/// @param appId appId
/// @param appKey appKey
/// @param activeCode 和登录的 userId 绑定的激活码
- (void)checkActiveWithAppId:(NSString *)appId
                      appKey:(NSString *)appKey
                  activeCode:(NSString *)activeCode;

/// 退出账号的时候调用.
- (void)logout;

#pragma mark - ======== 家庭管理 ========
/// 启动一个家庭的服务
/// @param homeId 家庭参数
- (BOOL)startHome:(NSString *)homeId;

/// 启动一组家庭的服务, 不在该数组中的家庭将会停止服务.
- (void)startHomes:(NSArray <NSString *> *)homeIds;

/// 停止服务,保留相关记录
/// @param homeId 家庭参数
- (void)stopHome:(NSString *)homeId;

/// 停止所有家庭的服务
- (void)stopAllHome;

/// 移除家庭服务, 停止服务并删除此家庭的所有记录
- (void)removeHome:(NSString *)homeId;

/// 获得当前已有服务的 homeId
/// 返回值: 一个包含 homeId 的数组.
- (NSArray<NSString *> *)getConnectingHomes;

@end

NS_ASSUME_NONNULL_END
